<?php

namespace SLN0001\Controller;

use Home\Service\UserService;
use Home\Common\FIdConst;
use Home\Controller\PSIBaseController;
use SLN0001\Service\VatService;

/**
 * 增值税发票Controller
 *
 * @author PSI
 * @copyright 2015 - present
 * @license GPL v3
 */
class VatController extends PSIBaseController
{
  /**
   * 增值税发票进项管理 - 主页面
   * 
   * HTML模板：web\Application\SLN0001\View\Vat\inIndex.html
   */
  public function inIndex()
  {
    $us = new UserService();

    if ($us->hasPermission(FIdConst::VAT_IN)) {
      $this->initVar();

      $this->assign("title", "增值税发票进项管理");

      // 按钮权限：新建采购订单
      $this->assign("pAdd", $us->hasPermission(FIdConst::VAT_IN_ADD) ? "1" : "0");
      // 按钮权限：编辑采购订单
      $this->assign("pEdit", $us->hasPermission(FIdConst::VAT_IN_EDIT) ? "1" : "0");
      // 按钮权限：删除采购订单
      $this->assign(
        "pDelete",
        $us->hasPermission(FIdConst::VAT_IN_DELETE) ? "1" : "0"
      );
      // 按钮权限：上传电子发票
      $this->assign("pUploadPDF", $us->hasPermission(FIdConst::VAT_IN_UPLOAD_PDF) ? "1" : "0");
      // 按钮权限：查看电子发票
      $this->assign("pDownloadPDF", $us->hasPermission(FIdConst::VAT_IN_DOWNLOAD_PDF) ? "1" : "0");

      // 按钮权限：新建关联
      $this->assign("pRelAdd", $us->hasPermission(FIdConst::VAT_IN_REL_ADD) ? "1" : "0");
      // 按钮权限：删除关联
      $this->assign("pRelDelete", $us->hasPermission(FIdConst::VAT_IN_REL_DELETE) ? "1" : "0");
      // 按钮权限：核销
      $this->assign("pRelCommit", $us->hasPermission(FIdConst::VAT_IN_COMMIT) ? "1" : "0");

      $this->display();
    } else {
      $this->gotoLoginPage("/SLN0001/Vat/inIndex");
    }
  }

  /**
   * 增值税发票销项管理 - 主页面
   * 
   * HTML模板：web\Application\SLN0001\View\Vat\outIndex.html
   */
  public function outIndex()
  {
    $us = new UserService();

    if ($us->hasPermission(FIdConst::VAT_IN)) {
      $this->initVar();

      $this->assign("title", "增值税发票销项管理");

      $this->display();
    } else {
      $this->gotoLoginPage("/SLN0001/Vat/outIndex");
    }
  }

  /**
   * 返回所有的公司列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\IncomeMainForm.js
   */
  public function companyListForVatIncome()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::VAT_IN);
      if (!$hasPermission) {
        die("没有权限");
      }

      $service = new VatService();
      $this->ajaxReturn($service->companyList(FIdConst::VAT_IN));
    }
  }

  /**
   * 返回所有的公司列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\OutputMainForm.js
   */
  public function companyListForVatOutput()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::VAT_OUT);
      if (!$hasPermission) {
        die("没有权限");
      }

      $service = new VatService();
      $this->ajaxReturn($service->companyList(FIdConst::VAT_OUT));
    }
  }

  /**
   * 增值税进项发票列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\IncomeMainForm.js
   */
  public function incomeList()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::VAT_IN);
      if (!$hasPermission) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "status" => I("post.status"),
        "ref" => I("post.ref"),
        "fromDT" => I("post.fromDT"),
        "toDT" => I("post.toDT"),
        "supplierId" => I("post.supplierId"),
        "category" => I("post.category"),

        "start" => I("post.start"),
        "limit" => I("post.limit")
      ];

      $service = new VatService();
      $this->ajaxReturn($service->incomeList($params));
    }
  }

  /**
   * 新建或编辑进项发票记录
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\IncomeEditForm.js
   */
  public function editVatIncome()
  {
    if (IS_POST) {
      $us = new UserService();

      $id = I("post.id");

      if ($id) {
        // 编辑
        $hasPermission = $us->hasPermission(FIdConst::VAT_IN_EDIT);
        if (!$hasPermission) {
          die("没有权限");
        }
      } else {
        // 新建
        $hasPermission = $us->hasPermission(FIdConst::VAT_IN_ADD);
        if (!$hasPermission) {
          die("没有权限");
        }
      }


      $params = [
        "id" => $id,
        "companyId" => I("post.companyId"),
        "ref" => I("post.ref"),
        "status" => I("post.status"),
        "category" => I("post.category"),
        "dt" => I("post.dt"),
        "supplierId" => I("post.supplierId"),
        "goodsMoney" => I("post.goodsMoney"),
        "tax" => I("post.tax"),
        "memo" => I("post.memo"),
      ];

      $service = new VatService();
      $this->ajaxReturn($service->editVatIncome($params));
    }
  }

  /**
   * 删除进项发票记录
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\IncomeMainForm.js
   */
  public function deleteVatIncome()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::VAT_IN_DELETE);
      if (!$hasPermission) {
        die("没有权限");
      }


      $params = [
        "id" => I("post.id"),
      ];

      $service = new VatService();
      $this->ajaxReturn($service->deleteVatIncome($params));
    }
  }

  /**
   * 查询进项发票记录的详情
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\IncomeEditForm.js
   */
  public function vatIncomeInfo()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::VAT_IN_EDIT);
      if (!$hasPermission) {
        die("没有权限");
      }


      $params = [
        "id" => I("post.id"),
      ];

      $service = new VatService();
      $this->ajaxReturn($service->vatIncomeInfo($params));
    }
  }

  /**
   * 上传进项电子发票
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\IncomeImportForm.js
   */
  public function uploadIncomePDF()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::VAT_IN_UPLOAD_PDF);
      if (!$hasPermission) {
        die("没有权限");
      }

      // PHP文件上传的技术参考文档
      // https://www.runoob.com/php/php-file-upload.html
      // 具体的文件流数据在PHP的全局变量里面
      // 上传文件没有使用ThinkPHP的内置功能，目的是减少对ThinkPHP的依赖，方便以后升级PHP运行时版本

      // 另外MySQL里面需要配置max_allowed_packet为适当的值
      // [mysqld]
      // max_allowed_packet = 16M

      $params = [
        "id" => I("post.id"),
      ];

      $service = new VatService();
      $this->ajaxReturn($service->uploadIncomePDF($params));
    }
  }

  /**
   * 查看进项电子发票
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\IncomeMainForm.js
   */
  public function downloadIncomePDF()
  {
    // ------------------------------
    // 本action是个GET请求
    // ------------------------------

    $us = new UserService();

    $hasPermission = $us->hasPermission(FIdConst::VAT_IN_DOWNLOAD_PDF);
    if (!$hasPermission) {
      die("没有权限");
    }

    $params = [
      "id" => I("get.id"),
    ];

    $service = new VatService();
    $this->ajaxReturn($service->downloadIncomePDF($params));
  }

  /**
   * 获得采购入库单主表列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\SelectPurchaseForm.js
   */
  public function pwbillList()
  {
    if (IS_POST) {
      $us = new UserService();
      if (!$us->hasPermission(FIdConst::VAT_IN_REL_ADD)) {
        die("没有权限");
      }

      $service = new VatService();
      $params = [
        "ref" => I("post.ref"),
        "supplierId" => I("post.supplierId"),
        "start" => I("post.start"),
        "limit" => I("post.limit")
      ];

      $this->ajaxReturn($service->pwbillList($params));
    }
  }

  /**
   * 获得采购入库单的明细记录
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\SelectPurchaseForm.js
   */
  public function pwBillDetailList()
  {
    if (IS_POST) {
      $us = new UserService();
      if (!$us->hasPermission(FIdConst::VAT_IN_REL_ADD)) {
        die("没有权限");
      }

      $pwbillId = I("post.pwBillId");
      $ps = new VatService();
      $this->ajaxReturn($ps->pwBillDetailList($pwbillId));
    }
  }

  /**
   * 获得采购退货出库单主表信息列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\SelectPurchaseForm.js
   */
  public function prbillList()
  {
    if (IS_POST) {
      $us = new UserService();
      if (!$us->hasPermission(FIdConst::VAT_IN_REL_ADD)) {
        die("没有权限");
      }

      $params = [
        "ref" => I("post.ref"),
        "supplierId" => I("post.supplierId"),
        "start" => I("post.start"),
        "limit" => I("post.limit")
      ];

      $pr = new VatService();
      $this->ajaxReturn($pr->prbillList($params));
    }
  }

  /**
   * 采购退货出库单的明细
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\SelectPurchaseForm.js
   */
  public function prBillDetailList()
  {
    if (IS_POST) {
      $us = new UserService();
      if (!$us->hasPermission(FIdConst::VAT_IN_REL_ADD)) {
        die("没有权限");
      }

      $params = [
        "id" => I("post.id")
      ];

      $pr = new VatService();

      $this->ajaxReturn($pr->prBillDetailList($params));
    }
  }

  /**
   * 新建关联
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\SelectPurchaseForm.js
   */
  public function addRelation()
  {
    if (IS_POST) {
      $us = new UserService();
      if (!$us->hasPermission(FIdConst::VAT_IN_REL_ADD)) {
        die("没有权限");
      }

      $params = [
        "vatId" => I("post.vatId"),
        "billType" => I("post.billType"),
        "billId" => I("post.billId"),
      ];

      $pr = new VatService();

      $this->ajaxReturn($pr->addRelation($params));
    }
  }

  /**
   * 删除关联
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\IncomeMainForm.js
   */
  public function deleteRelation()
  {
    if (IS_POST) {
      $us = new UserService();
      if (!$us->hasPermission(FIdConst::VAT_IN_REL_DELETE)) {
        die("没有权限");
      }

      $params = [
        "id" => I("post.id"),
      ];

      $pr = new VatService();

      $this->ajaxReturn($pr->deleteRelation($params));
    }
  }

  /**
   * 查询发票和单据关联列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\IncomeMainForm.js
   */
  public function incomeBillList()
  {
    if (IS_POST) {
      $us = new UserService();
      if (!$us->hasPermission(FIdConst::VAT_IN)) {
        die("没有权限");
      }

      $params = [
        // 发票记录id
        "id" => I("post.id"),
      ];

      $pr = new VatService();

      $this->ajaxReturn($pr->incomeBillList($params));
    }
  }

  /**
   * 核销
   * 
   * JS: web\Public\Scripts\PSI\SLN0001\Vat\IncomeMainForm.js
   */
  public function commitVatIncome()
  {
    if (IS_POST) {
      $us = new UserService();
      if (!$us->hasPermission(FIdConst::VAT_IN_COMMIT)) {
        die("没有权限");
      }

      $params = [
        // 发票记录id
        "id" => I("post.id"),
      ];

      $pr = new VatService();

      $this->ajaxReturn($pr->commitVatIncome($params));
    }
  }
}
